﻿using NUnit.Framework;
using SyringeControl.ControlSamples;
using System.Linq.Expressions;
using System;

namespace SyringeControl.WinForms.Tests
{
    [TestFixture]
    public class Test_BindingExtensions_OnChange
    {
        SampleModel model;
        [SetUp]
        public void SetUp()
        {
            model = new SampleModel();
        }

        [Test]
        public void OnChange_causes_action_executing_when_property_changes()
        {
            var wasCalled = false;
            model.OnChange(m => m.StringField, s => wasCalled = true);
            model.StringField = "hello";
            Assert.That(wasCalled);
        }

        [Test]
        public void There_could_be_multiple_OnChange_handlers()
        {
            var wasCalled = false;
            var str = "";
            model.OnChange(m => m.StringField, s => wasCalled = true);
            model.OnChange(m => m.StringField, s => str = s);
            model.StringField = "hello";
            Assert.That(wasCalled);
            Assert.That(str, Is.EqualTo(model.StringField));
        }

        [TestCase(true,true, true)]
        [TestCase(false,true, true, ExpectedException=typeof(ArgumentNullException))]
        [TestCase(true,false, true, ExpectedException=typeof(ArgumentNullException))]
        [TestCase(true,true, false, ExpectedException=typeof(ArgumentNullException))]
        public void OnChange_checks_parameters_on_nullness(bool hasModel, bool hasModelProperty, bool hasAction)
        {
            model = hasModel ? model : null;
            var prop = hasModelProperty ? (Expression<Func<SampleModel, string>>)(m => m.StringField) : null;
            var action = hasAction? new Action<string>(delegate{}) : null;
            model.OnChange(prop, action);
        }

        [Test, ExpectedException(typeof(ArgumentException))]
        public void OnChange_checks_that_modeProperty_argument_describes_property()
        {
            model.OnChange(m => m.GetType(), delegate { });
        }
    }
}
